package com.cater.sys.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.cater.annotation.SystemControllerLog;
import com.cater.common.PageResult;
import com.cater.query.CommonQuery;
import com.cater.sys.entity.CompanyOrganize;
import com.cater.sys.service.CompanyOrganizeServiceImpl;
import com.cater.utils.TreeBuilder;
import com.cater.utils.TreeBuilder.Node;

/**
 * 描述：组织架构控制层
 * 
 * @author yong
 * @date 2019/03/07
 */
@Controller
@RequestMapping("/organize")
public class CompanyOrganizeController {

	@Autowired
	private CompanyOrganizeServiceImpl service;

	/**
	 * @组织架构管理列表视图
	 * @return
	 */
	@RequestMapping("/list")
	@RequiresPermissions("organize:list")
	public String list(String companyId, Model model) {
		model.addAttribute("companyId", companyId);
		return "company/organize/list";
	}

	/**
	 * @组织架构管理数据分页
	 * @param query
	 * @param request
	 * @return
	 */
	@RequestMapping("listData")
	@ResponseBody
	@SystemControllerLog(description = "查询组织架构分页数据")
	public PageResult<CompanyOrganize> listData(CommonQuery query, HttpServletRequest request) {
		PageResult<CompanyOrganize> result = service.findPageResult(query);
		return result;
	}

	/**
	 * 添加视图
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "addView")
	@RequiresPermissions("organize:add")
	public String addView(String companyId, String pid, Model model) {
		model.addAttribute("companyId", companyId);
		model.addAttribute("pid", pid);
		return "company/organize/add";
	}

	/**
	 * 添加操作
	 * 
	 * @param t
	 * @return
	 */
	@RequestMapping(value = "add")
	@RequiresPermissions("organize:add")
	@ResponseBody
	@SystemControllerLog(description = "添加组织架构")
	public JSONObject add(CompanyOrganize t, CommonQuery query) {
		JSONObject json = new JSONObject();
		String pid = t.getPid();
		String id = null;
		id = service.saves(t);
		if (StringUtils.isNotBlank(id)) {
			if (pid.equals("0")) {
				t.setTreeIds(id);
				t.setTreeNames(t.getName());
			} else {
				query.setPid(t.getPid());
				CompanyOrganize pOrganize = service.getByPid(query);
				t.setTreeIds(pOrganize.getTreeIds() + "," + t.getId());
				t.setTreeNames(pOrganize.getTreeNames() + "/" + t.getName());
			}
			service.update(t);
			json.put("code", 200);
			json.put("message", "success");
		} else {
			json.put("code", 999);
			json.put("message", "fail");
		}
		return json;
	}

	/**
	 * 修改视图
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "editView")
	@RequiresPermissions("organize:edit")
	public String editView(String id, Model model, CommonQuery query) {
		CompanyOrganize t = service.getById(id);
		model.addAttribute("organize", t);
		return "company/organize/update";
	}

	/**
	 * 修改操作
	 * 
	 * @param t
	 * @return
	 */
	@RequestMapping(value = "update")
	@RequiresPermissions("organize:edit")
	@ResponseBody
	@SystemControllerLog(description = "修改组织架构")
	public JSONObject update(CompanyOrganize t, CommonQuery query) {
		JSONObject json = new JSONObject();
		try {
			service.update(t);
			json.put("code", 200);
			json.put("message", "修改成功");
		} catch (Exception e) {
			e.printStackTrace();
			json.put("code", 999);
			json.put("message", "修改失败");
		}
		return json;
	}

	/**
	 * 删除操作
	 * 
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/delete")
	@RequiresPermissions("organize:delete")
	@SystemControllerLog(description = "删除组织架构")
	public JSONObject delete(String id) {
		JSONObject json = new JSONObject();
		try {
			service.delete(id);
			json.put("code", 200);
			json.put("message", "删除成功");
		} catch (Exception e) {
			e.printStackTrace();
			json.put("code", 999);
			json.put("message", "删除失败");
		}
		return json;
	}

	/**
	 * 查看视图
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "detailView")
	@RequiresPermissions("organize:detail")
	public String detailView(String id, Model model, CommonQuery query) {
		CompanyOrganize t = service.getById(id);
		model.addAttribute("organize", t);
		return "company/organize/detail";
	}

	/**
	 * 查看组织架构图
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "orgView")
	public String orgView(String companyId, Model model) {
		model.addAttribute("companyId", companyId);
		return "company/organize/orgChart";
	}

	/**
	 * 获取公司组织数据
	 * 
	 * @param query
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "getByCompanyId", method = RequestMethod.GET)
	public String getListBy(CommonQuery query) {
		String result = null;
		try {
			List<CompanyOrganize> lists = service.getListBy(query);
			List<Node> nodes = new ArrayList<TreeBuilder.Node>();
			for (CompanyOrganize companyOrganize : lists) {
				Node node = new Node();
				node.setId(companyOrganize.getId());
				node.setName(companyOrganize.getName());
				node.setPid(companyOrganize.getPid());
				nodes.add(node);
			}
			// 拼装树形json字符串
			result = new TreeBuilder().buildTree(nodes);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;

	}

}